#!/usr/bin/env python
# encoding: utf-8

__author__ = 'huangyu'

import boto3


class ClassChangeIp(object):
    def __init__(self, **kwargs):
        self.session = boto3.Session(**kwargs)

        self.ec2 = self.session.resource("ec2")

    def change_ip(self, elastic_ip, instance_ids_list):
        """
        change IP for aws
        :param elastic_ip: String: 弹性IP地址
        :param instance_ids_list: List: 需要换IP的机器实例的id的列表
        :return:None
        """
        classic_address = self.ec2.ClassicAddress(public_ip=elastic_ip)
        if not elastic_ip or not instance_ids_list or not isinstance(instance_ids_list, list):
            raise Exception("参数错误")

        # 换ip
        for a_instance_id in instance_ids_list:
            classic_address.associate(InstanceId=a_instance_id)

        # 解除绑定
        classic_address.disassociate()

        return


if __name__ == '__main__':
    a_ip = '54.222.131.126'
    ids_list = ['i-106832a8']

    req_kwargs = {
        "aws_access_key_id": "AKIAPVVJJO6TN7NUIFEQ",
        "aws_secret_access_key": "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",
        "region_name": "cn-north-1",
    }
    c_ip = ClassChangeIp()
    # or
    # c_ip = ClassChangeIp(**req_kwargs)

    c_ip.change_ip(a_ip, ids_list)




